package com.code;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author cj
 * @date 2018/11/15
 */

/*
@Configuration
@ComponentScan("com.code")
@EnableScheduling
public class AppConfig {
}
*/


// 下面这段代码是从EnableScheduling注解的注释中copy过来的
    // 主要是让任务执行在线程池中,起到异步执行的效果
@Configuration
@EnableScheduling
@ComponentScan("com.code")
public class AppConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }
}